<?php
	/**
	* Product Categories - Save
	* @author trungdt
	* @since Sep 27, 2011
	* 
	* Change logs
	* 
	*/

	if (!defined('EXPONENT')) exit('');

	$cat = null;
	if (isset($_POST['id'])) {
		$cat = $db->selectObject('productsmodule_category','id='.intval($_POST['id']));
		if ($cat) {
			$loc = unserialize($cat->location_data);
		}
	} else {
		$cat->rank = $db->max('productsmodule_category', 'rank', 'location_data', "location_data='".serialize($loc)."'");
		if ($cat->rank == null) {
			$cat->rank = 0;
		} else {
			$cat->rank += 1;
		}
	}

	if (exponent_permissions_check(array('administrate','configure'),$loc)) {
		$cat = productsmodule_categories::categories_update($_POST,$cat);
		$cat->location_data = serialize($loc);

		if (isset($cat->id)) {
			$db->updateObject($cat,'productsmodule_category');
		} else {
			$db->insertObject($cat,'productsmodule_category');
		}
        
        // clear category cache
        exponent_sessions_set("__PDoremiShop_CategorySeoNameBuild_Cats", null);
		exponent_flow_redirect();
	} else {
		echo SITE_404_HTML;
	}

?>